package com.example.demo.controller;

import com.example.demo.dto.CommentTeacherDto;
import com.example.demo.dto.DepartmentDto;
import com.example.demo.dto.StudentDto;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Department;
import com.example.demo.mapper.CommentTeacherMapper;
import com.example.demo.mapper.DepartmentMapper;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("teacher")
public class TeacherController {

    @Autowired(required = false)
    private TeacherMapper teacherMapper;
    @Autowired(required = false)
    private DepartmentMapper departmentMapper;
    @Autowired(required = false)
    private CommentTeacherMapper commentTeacherMapper;

    //教师课程管理
    @RequestMapping("myDepPage")
    public ResultUtil getMyDepList(HttpSession session, Integer page, Integer limit){
        Integer id = (Integer) session.getAttribute("id");
        page=(page-1)*limit;
        List<DepartmentDto> departmentDtos = teacherMapper.selectMyDepPage(page, limit, id);
        Integer count = teacherMapper.selectMyDepCount(id);
        return new ResultUtil(0,"成功",count,departmentDtos);
    }

    //添加课程
    @RequestMapping("addClass")
    public ResultUtil addDep(HttpSession session,Department department){
        Integer tId = (Integer) session.getAttribute("id");
        department.setCreateby(tId);
        Integer integer = departmentMapper.insert(department);
        if (integer>0){
            return new ResultUtil(200,"添加成功",null,null);
        }
        return new ResultUtil(500,"添加失败",null,null);
    }

    //删除课程
    @RequestMapping("delDep")
    @Transactional
    public ResultUtil delDep(Integer depId){
        Integer integer = departmentMapper.delById(depId);
        //删除对应的学生选课表
        teacherMapper.delSDByDId(depId);
        if (integer>0){
            return new ResultUtil(200,"删除成功",null,null);
        }
        return new ResultUtil(500,"删除失败",null,null);
    }



    //教师学生管理
    @RequestMapping("stuPage")
    public ResultUtil stuPage(HttpSession session, Integer page, Integer limit){
        page=(page-1)*limit;
        Integer tId = (Integer) session.getAttribute("id");
        List<StudentDto> studentDtos = teacherMapper.stuPage(page, limit, tId);
        Integer count = teacherMapper.stuPageCount(tId);
        return new ResultUtil(0,"成功",count,studentDtos);
    }

    //添加学生评价
    @RequestMapping("addComment")
    public ResultUtil addComment(HttpSession session, Comment comment){
        System.out.println(comment);
        Integer id = (Integer) session.getAttribute("id");
        //查询是否已经存在评价
        Integer integer = commentTeacherMapper.selectBySIdAndDepId(id, comment.getDepartmentId());
        if (integer > 0) {
            return new ResultUtil(500,"已经评价过此学生",null,null);
        }
        //添加评价
        comment.setTeacherId(id);
        Integer integer1 = commentTeacherMapper.insertComment(comment);
        if (integer1>0){
            return new ResultUtil(200,"评价成功",null,null);
        }
        return new ResultUtil(500,"未知错误",null,null);
    }
    //修改学生评价
    @RequestMapping("editComment")
    public ResultUtil editComment(Integer id,String content,Integer score){
        Integer integer = teacherMapper.updateComment(content, score, id);
        if (integer>0){
            return new ResultUtil(200,"修改评价成功",null,null);
        }
        return new ResultUtil(500,"修改评价失败",null,null);
    }

    //删除学生评价
    @RequestMapping("delComment")
    public ResultUtil delComment(Integer id,String content,Integer score){
        Integer integer = commentTeacherMapper.delComment(id);
        if (integer>0){
            return new ResultUtil(200,"删除评价成功",null,null);
        }
        return new ResultUtil(500,"删除评价失败",null,null);
    }

    //我的评价页面
    @RequestMapping("myCommentPage")
    public ResultUtil myCommentPage(HttpSession session,Integer page,Integer limit){
        Integer tId = (Integer) session.getAttribute("id");
        page = (page-1)*limit;
        List<CommentTeacherDto> commentTeacherDtos = teacherMapper.selectMyComment(page, limit, tId);
        Integer count = teacherMapper.selectMyCommentCount(tId);
        return new ResultUtil(0,"查询成功",count,commentTeacherDtos);

    }
}
